//
//  UIDevice+TSHSystemInfo.m
//  TSHui
//
//  Created by Soto on 16/7/2.
//  Copyright © 2016年 TeSheHui. All rights reserved.
//

#import "UIDevice+TSHSystemInfo.h"

#include <spawn.h>

@implementation UIDevice (TSHSystemInfo)

+ (NSString *)SystemVersion
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice currentDevice].systemVersion;

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return nil;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (NSString *)OSVersion
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [NSString stringWithFormat:@"%@ %@", [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return nil;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (NSString *)OSLanguage
{
	NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
	NSArray *languages = [defs objectForKey:@"AppleLanguages"];
	NSString *preferredLang = [languages objectAtIndex:0];

	return preferredLang;
}

+ (NSString *)appDisplayName
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR || TARGET_OS_MAC)
	static NSString *__appName = nil;

	if (nil == __appName) {
		__appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
	}

	return __appName;

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return nil;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (NSString *)appBuildVersion
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR || TARGET_OS_MAC)
	NSString *value = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

	if ((nil == value) || (0 == value.length)) {
		value = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
	}

	return value;

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return nil;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (NSString *)appShortVersion
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR || TARGET_OS_MAC)
	NSString *value = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

	if ((nil == value) || (0 == value.length)) {
		value = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
	}

	return value;

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return nil;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (NSString *)appIdentifier
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	static NSString *__identifier = nil;

	if (nil == __identifier) {
		__identifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
	}

	return __identifier;

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return @"";
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (NSString *)deviceModel
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice currentDevice].model;

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return nil;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
static const char *__jb_app = NULL;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)

+ (BOOL)isJailBroken NS_AVAILABLE_IOS(4_0)
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	static const char *__jb_apps[] = {
		"/Application/Cydia.app",
		"/Application/limera1n.app",
		"/Application/greenpois0n.app",
		"/Application/blackra1n.app",
		"/Application/blacksn0w.app",
		"/Application/redsn0w.app",
		NULL
	};

	__jb_app = NULL;

	// method 1
	for (int i = 0; __jb_apps[i]; ++i) {
		if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:__jb_apps[i]]]) {
			__jb_app = __jb_apps[i];
			return YES;
		}
	}

	// method 2
	if ([[NSFileManager defaultManager] fileExistsAtPath:@"/private/var/lib/apt/"]) {
		return YES;
	}

	// method 3
	char *const args[] = {"ls", "-la", NULL};
	int err;

	err = posix_spawn(NULL, "/bin/ls", NULL, NULL, args, NULL);

	if (err != 0) {
		return YES;
	}
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)

	return NO;
}

+ (NSString *)jailBreaker NS_AVAILABLE_IOS(4_0)
{
#if (TARGET_OS_IPHONE)
	if (__jb_app) {
		return [NSString stringWithUTF8String:__jb_app];
	}
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)

	return @"";
}

+ (BOOL)isDevicePhone
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	NSString *deviceType = [UIDevice currentDevice].model;

	if (([deviceType rangeOfString:@"iPhone" options:NSCaseInsensitiveSearch].length > 0) ||
		([deviceType rangeOfString:@"iPod" options:NSCaseInsensitiveSearch].length > 0) ||
		([deviceType rangeOfString:@"iTouch" options:NSCaseInsensitiveSearch].length > 0)) {
		return YES;
	}
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)

	return NO;
}

+ (BOOL)isDevicePad
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	NSString *deviceType = [UIDevice currentDevice].model;

	if ([deviceType rangeOfString:@"iPad" options:NSCaseInsensitiveSearch].length > 0) {
		return YES;
	}
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)

	return NO;
}

+ (BOOL)isRetina2x
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	if ([[UIScreen mainScreen] scale] == 2) {
		return YES;
	}
	else {
		return NO;
	}

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isRetina3x
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	if ([[UIScreen mainScreen] scale] == 3) {
		return YES;
	}
	else {
		return NO;
	}

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhone35
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice isScreenSize:CGSizeMake(320, 480)];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhoneRetina35
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice isScreenSize:CGSizeMake(640, 960)];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhoneRetina4
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice isScreenSize:CGSizeMake(640, 1136)];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhoneRetinaHD47
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice isScreenSize:CGSizeMake(750, 1334)];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhoneRetinaHD55
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice isScreenSize:CGSizeMake(1242, 2208)];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhoneRetinaIphoneX
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    return [UIDevice isScreenSize:CGSizeMake(1125, 2436)];
    
#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhoneRetinaIphoneXr
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    return [UIDevice isScreenSize:CGSizeMake(828, 1792)];
    
#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPhoneRetinaIphoneXsMAX
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    return [UIDevice isScreenSize:CGSizeMake(1242, 2688)];
    
#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
    return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPad
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice isScreenSize:CGSizeMake(768, 1024)];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isPadRetina
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return [UIDevice isScreenSize:CGSizeMake(1536, 2048)];

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

+ (BOOL)isScreenSize:(CGSize)size
{
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	if ([UIScreen instancesRespondToSelector:@selector(currentMode)]) {
		CGSize screenSize = [UIScreen mainScreen].currentMode.size;
		CGSize size2 = CGSizeMake(size.height, size.width);

		if (CGSizeEqualToSize(size, screenSize) || CGSizeEqualToSize(size2, screenSize)) {
			return YES;
		}
	}

	return NO;

#else // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
	return NO;
#endif // #if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
}

@end
